package com.huangyi.rabbitmq_amqp.work;

import com.huangyi.rabbitmq_amqp.config.Constants;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.huangyi.rabbitmq_amqp.config.Constants;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(Constants.HOST);
        factory.setPort(Constants.PORT);
        factory.setVirtualHost(Constants.VIRTUAL_HOST);
        factory.setUsername(Constants.USER);
        factory.setPassword(Constants.PASSWORD);

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(Constants.WORK_QUEUE1, true, false, false, null);
        channel.queueDeclare(Constants.WORK_QUEUE2, true, false, false, null);
        for (int i = 0; i < 10; i++) {
            String msg = "hello " + i;
            channel.basicPublish("", Constants.WORK_QUEUE1, null, msg.getBytes());
            channel.basicPublish("",Constants.WORK_QUEUE2,null,msg.getBytes());
        }
        System.out.println("消息发送成功");

        channel.close();
        connection.close();
    }

}
